﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FineUI;

public partial class Manager_SysSetting : System.Web.UI.PageEx
{
    private string strPageCode = "S0000A";
    private string strLoginResult = string.Empty;
    private string strSelectLang = "CN";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            ViewState["SelectLang"] = strSelectLang;
            DataBinds();
        }
    }
    private void DataBinds()
    {
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            txtWebName.Text = dbSystem.SWebName;
            txtTitle.Text = dbSystem.STitle;
            txtKeywords.Text = dbSystem.SKeywords;
            txtDescription.Text = dbSystem.SDescription;
            txtAddress.Text = dbSystem.SAddress;
            txtContactName.Text = dbSystem.SContactName;
            txtWeb400.Text = dbSystem.SWeb400;
            txtWebUrl.Text = dbSystem.SWebUrl;
            txtEmail.Text = dbSystem.SEmail;
            cboxBaoMingState.Checked = dbSystem.BaoMingState == 1 ? true : false;
            //cboxUrlRewrite.Checked = Convert.ToBoolean(dbSystem.SIsUrlReWrite);
            cboxWebStatus.Checked = Convert.ToBoolean(dbSystem.SWebStatus);

            PageContext.RegisterStartupScript(String.Format("updateUEditor1({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbSystem.SWebStopAlter))));
            PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbSystem.SBottom))));

            fuLogo.EmptyText = "不修改请置空！";
            fuWebIcon.EmptyText = "不修改请置空！";
            fuWeChat.EmptyText = "不修改请置空！";
            fuWeibo.EmptyText = "不修改请置空！";
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        strSelectLang = Convert.ToString(ViewState["SelectLang"]);

        string strWebName = txtWebName.Text.Trim();
        string strTitle = txtTitle.Text.Trim();
        string strKeywords = txtKeywords.Text.Trim();
        string strDescription = txtDescription.Text.Trim();
        string strIcon = string.Empty;
        string strLogo = string.Empty;
        string strWeChat = string.Empty;
        string strWeibo = string.Empty;
        string strContactName = txtContactName.Text.Trim();
        string strWeb400 = txtWeb400.Text.Trim();
        string strWebUrl = txtWebUrl.Text.Trim();
        string strAddress = txtAddress.Text.Trim();
        string strEmail = txtEmail.Text.Trim();
        string strBottom = Request.Form["UEditor2"].ToString(); ;
        short sIsUrlRewrite = 0;
        short sWebStatus = Convert.ToInt16(cboxWebStatus.Checked);
        string strWebStopAlter = Request.Form["UEditor1"].ToString(); ;
        int iBaoMingState = cboxBaoMingState.Checked ? 1 : 0;
        int iResult = 0;
        string _strActionResult = string.Empty;

        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem == null)
        {
            dbSystem = new Model.DbSystem();
            dbSystem.SBottom = SqlHelper.String(strBottom);
            dbSystem.SWebStopAlter = SqlHelper.String(strWebStopAlter);
        }
        else
        {
            if (!string.IsNullOrEmpty(strBottom))
                dbSystem.SBottom = SqlHelper.String(strBottom);
            if (!string.IsNullOrEmpty(strWebStopAlter))
                dbSystem.SWebStopAlter = SqlHelper.String(strWebStopAlter);

        }
        dbSystem.SWebName = SqlHelper.String(strWebName);
        dbSystem.STitle = SqlHelper.String(strTitle);
        dbSystem.SKeywords = SqlHelper.String(strKeywords);
        dbSystem.SDescription = SqlHelper.String(strDescription);
        strIcon = WebBase.Comm.GetFilePath(fuWebIcon, Server.MapPath("~/upload/system/"), ResolveUrl("~/upload/system/"), "*.ico|*.png", ref _strActionResult);
        if (!string.IsNullOrEmpty(strIcon))
            dbSystem.SIcon = SqlHelper.String(strIcon);
        strLogo = WebBase.Comm.GetFilePath(fuLogo, Server.MapPath("~/upload/system/"), ResolveUrl("~/upload/system/"), "*.gif|*.jpg|*.png|*.jpeg", ref _strActionResult);
        if (!string.IsNullOrEmpty(strLogo))
            dbSystem.SLogo = SqlHelper.String(strLogo);
        strWeChat = WebBase.Comm.GetFilePath(fuWeChat, Server.MapPath("~/upload/system/"), ResolveUrl("~/upload/system/"), "*.gif|*.jpg|*.png|*.jpeg", ref _strActionResult);
        if (!string.IsNullOrEmpty(strWeChat))
            dbSystem.WeChat = SqlHelper.String(strWeChat);
        strWeibo = WebBase.Comm.GetFilePath(fuWeibo, Server.MapPath("~/upload/system/"), ResolveUrl("~/upload/system/"), "*.gif|*.jpg|*.png|*.jpeg", ref _strActionResult);
        if (!string.IsNullOrEmpty(strWeibo))
            dbSystem.Weibo = SqlHelper.String(strWeibo);

        dbSystem.SContactName = SqlHelper.String(strContactName);
        dbSystem.SWeb400 = SqlHelper.String(strWeb400);
        dbSystem.SWebUrl = SqlHelper.String(strWebUrl);
        dbSystem.SAddress = SqlHelper.String(strAddress);
        dbSystem.SEmail = SqlHelper.String(strEmail);
        dbSystem.SIsUrlReWrite = sIsUrlRewrite;
        dbSystem.SWebStatus = sWebStatus;
        dbSystem.SEditTime = DateTime.Now;

        dbSystem.BaoMingState = iBaoMingState;

        if (dbSystem.SId > 0)
            iResult = Dal.DbSystemDal.Update(dbSystem);
        else
            iResult = Dal.DbSystemDal.Insert(dbSystem);

        if (iResult > 0)
        {
            DataBinds();
            FineUI.Alert.Show("站点基本信息已保存！", FineUI.MessageBoxIcon.Information);
        }
        else
        {
            FineUI.Alert.Show("站点基本信息保存失败！", FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnLoadData_Click(object sender, EventArgs e)
    {
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            PageContext.RegisterStartupScript(String.Format("updateUEditor1({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbSystem.SWebStopAlter))));
            PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbSystem.SBottom))));
        }
    }
}
